local zhiye = fk.CreateSkill{
  name = "hanqing__zhiye",
}

Fk:loadTranslationTable{
  ["hanqing__zhiye"] = "治邺",
  [":hanqing__zhiye"] = "出牌阶段开始时，你可以进行“<a href='zhengsu_desc'>整肃</a>”；你可以分配你因“<a href='zhengsu_desc'>整肃</a>”奖励"..
  "获得的牌，令获得牌的其他角色下个回合获得此技能。",

  ["#hanqing__zhiye-invoke"] = "治邺：你可以进行“整肃”，若成功，则弃牌阶段结束后获得奖励",
  ["#hanqing__zhiye-choice"] = "治邺：选择你本回合“整肃”的条件",
  ["#hanqing__zhiye-reward"] = "治邺：“整肃”成功，选择一项整肃奖励",
  ["#hanqing__zhiye-give"] = "治邺：你可以分配你的整肃奖励牌，获得这些牌的角色下回合拥有“治邺”",
}

local U = require "packages/utility/utility"

zhiye:addEffect(fk.EventPhaseStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhiye.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = zhiye.name,
      prompt = "#hanqing__zhiye-invoke",
    })
  end,
  on_use = function(self, event, target, player, data)
    U.startZhengsu(player, player, zhiye.name, "#hanqing__zhiye-choice")
  end,
})

zhiye:addEffect(fk.EventPhaseEnd, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Discard and not player.dead and
      U.checkZhengsu(player, target, zhiye.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"draw2"}
    if player:isWounded() then
      table.insert(choices, 1, "recover")
    end
    local reward = room:askToChoice(player, {
      choices = choices,
      skill_name = zhiye.name,
      prompt = "#hanqing__zhiye-reward",
      all_choices = {"draw2", "recover"},
    })
    U.rewardZhengsu(player, player, reward, zhiye.name)
  end,
})

zhiye:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(zhiye.name) and #player.room:getOtherPlayers(player, false) > 0 then
      for _, move in ipairs(data) do
        if move.to == player and move.moveReason == fk.ReasonDraw and move.toArea == Card.PlayerHand then
          --非常不严谨判断
          local skill_effect = player.room.logic:getCurrentEvent().parent
          if skill_effect and skill_effect.event == GameEvent.SkillEffect then
            local skill_name = skill_effect.data.skill:getSkeleton().name
            if string.find(Fk:translate(":"..skill_name, "zh_CN"), "整肃") then
              local ids = {}
              for _, info in ipairs(move.moveInfo) do
                if table.contains(player:getCardIds("h"), info.cardId) then
                  table.insertIfNeed(ids, info.cardId)
                end
              end
              if #ids > 0 then
                event:setCostData(self, {cards = ids})
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local result = room:askToYiji(player, {
      cards = cards,
      targets = room:getOtherPlayers(player, false),
      skill_name = zhiye.name,
      min_num = 0,
      max_num = #cards,
      prompt = "#hanqing__zhiye-give",
      skip = true,
    })
    for _, ids in pairs(result) do
      if #ids > 0 then
        event:setCostData(self, {extra_data = result})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local result = event:getCostData(self).extra_data
    room:doYiji(result, player, zhiye.name)
    for id, ids in pairs(result) do
      if #ids > 0 then
        local p = room:getPlayerById(id)
        if not p.dead then
          room:setPlayerMark(p, zhiye.name, 1)
        end
      end
    end
  end,
})

zhiye:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark(zhiye.name) > 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, zhiye.name, 0)
    if not player:hasSkill(zhiye.name, true) then
      room:handleAddLoseSkills(player, zhiye.name)
      room.logic:getCurrentEvent():findParent(GameEvent.Turn, true):addCleaner(function()
        room:handleAddLoseSkills(player, "-"..zhiye.name)
      end)
    end
  end,
})
return zhiye
